package com.sskj.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sskj.api.domain.ThingModelTemplate;
import com.sskj.api.exception.LogicException;
import com.sskj.api.mapper.ThingModelTemplateMapper;
import com.sskj.api.service.IThingModelTemplateService;
import com.sskj.api.vo.req.ThingModelTemplateReq;
import com.sskj.common.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

@Service
@Slf4j
public class ThingModelTemplateService extends ServiceImpl<ThingModelTemplateMapper, ThingModelTemplate> implements IThingModelTemplateService {

    @Override
    public void add(ThingModelTemplate modelTemplate) {
        LambdaQueryWrapper<ThingModelTemplate> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ThingModelTemplate::getName, modelTemplate.getName())
                .or()
                .eq(ThingModelTemplate::getIdentifier, modelTemplate.getIdentifier());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicException("名称或标识已存在");
        }

        modelTemplate.setCreateTime(LocalDateTime.now());
        save(modelTemplate);
    }

    @Override
    public void update(ThingModelTemplate modelTemplate) {
        LambdaQueryWrapper<ThingModelTemplate> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(ThingModelTemplate::getId, modelTemplate.getId()).and(consumer -> {
            consumer.eq(ThingModelTemplate::getName, modelTemplate.getName())
                    .or()
                    .eq(ThingModelTemplate::getIdentifier, modelTemplate.getIdentifier());
        });
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicException("名称或标识已存在");
        }

        modelTemplate.setUpdateTime(LocalDateTime.now());
        updateById(modelTemplate);
    }

    @Override
    public IPage<ThingModelTemplate> pageList(ThingModelTemplateReq req) {
        IPage<ThingModelTemplate> pageConfig = new Page<>(req.getPageNum(), req.getPageSize());

        LambdaQueryWrapper<ThingModelTemplate> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(ThingModelTemplate::getId);
        if (StrUtil.isNotBlank(req.getName())) {
            lambdaQueryWrapper.like(ThingModelTemplate::getName, req.getName());
        }
        if (StrUtil.isNotBlank(req.getIdentifier())) {
            lambdaQueryWrapper.like(ThingModelTemplate::getIdentifier, req.getIdentifier());
        }
        if (req.getType() != null && req.getType() > 0) {
            lambdaQueryWrapper.eq(ThingModelTemplate::getType, req.getType());
        }
        if (req.getStatus() != null && req.getStatus() >= 0) {
            lambdaQueryWrapper.eq(ThingModelTemplate::getStatus, req.getStatus());
        }
        return page(pageConfig, lambdaQueryWrapper);
    }
}
